package com.atguigu.gmall.realtime.app.dwm;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.realtime.bean.OrderWide;
import com.atguigu.gmall.realtime.bean.PaymentInfo;
import com.atguigu.gmall.realtime.bean.PaymentWide;
import com.atguigu.gmall.realtime.utils.DateUtils;
import com.atguigu.gmall.realtime.utils.KafkaUtils;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.restartstrategy.RestartStrategies;
import org.apache.flink.runtime.state.filesystem.FsStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * 支付宽表代码实现
 *
 * @author lvbingbing
 * @date 2022-06-29 16:52
 */
public class PaymentWideApp {
    public static void main(String[] args) throws Exception {
        // 1. 基本环境准备
        // 1.1 流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // 1.2 并行度设置
        int parallelism = 4;
        env.setParallelism(parallelism);
        // 2. 检查点相关设置
        // 2.1 开启检查点
        env.enableCheckpointing(10000L, CheckpointingMode.EXACTLY_ONCE);
        // 2.2 设置检查点超时时间
        CheckpointConfig checkpointConfig = env.getCheckpointConfig();
        checkpointConfig.setCheckpointTimeout(60000L);
        // 2.3 作业取消时保留检查点
        checkpointConfig.enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
        // 2.4 设置重启策略
        env.setRestartStrategy(RestartStrategies.fixedDelayRestart(3, 6000L));
        // 2.5 设置状态后端
        env.setStateBackend(new FsStateBackend("hdfs://hadoop102:8020/ck/gmall"));
        // 2.6 指定操作 hdfs 用户
        System.setProperty("HADOOP_USER_NAME", "atguigu");
        // 3. 从 kafka 读取数据，做结构转换，并指定时间戳和 watermark
        String groupId = "payment_wide_app_group";
        // 3.1 获取支付数据
        SingleOutputStreamOperator<PaymentInfo> paymentInfoDs = env.addSource(KafkaUtils.getKafkaSource("dwd_payment_info", groupId))
                .map(s -> JSON.parseObject(s, PaymentInfo.class))
                .assignTimestampsAndWatermarks(WatermarkStrategy.<PaymentInfo>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                        .withTimestampAssigner(new SerializableTimestampAssigner<PaymentInfo>() {
                            @Override
                            public long extractTimestamp(PaymentInfo element, long recordTimestamp) {
                                return DateUtils.getTimestamp(element.getCallback_time());
                            }
                        }));
        // 3.2 获取订单宽表数据
        SingleOutputStreamOperator<OrderWide> orderWideDs = env.addSource(KafkaUtils.getKafkaSource("dwm_order_wide", groupId))
                .map(s -> JSON.parseObject(s, OrderWide.class))
                .assignTimestampsAndWatermarks(WatermarkStrategy.<OrderWide>forBoundedOutOfOrderness(Duration.ofSeconds(3)).withTimestampAssigner(new SerializableTimestampAssigner<OrderWide>() {
                    @Override
                    public long extractTimestamp(OrderWide element, long recordTimestamp) {
                        return DateUtils.getTimestamp(element.getCreate_time());
                    }
                }));
        // 4. 双流join，获取支付宽表
        SingleOutputStreamOperator<PaymentWide> paymentWideInfo = paymentInfoDs.keyBy(PaymentInfo::getOrder_id)
                .intervalJoin(orderWideDs.keyBy(OrderWide::getOrder_id))
                .between(Time.minutes(-30), Time.minutes(0))
                .process(new ProcessJoinFunction<PaymentInfo, OrderWide, PaymentWide>() {
                    @Override
                    public void processElement(PaymentInfo paymentInfo, OrderWide orderWide, ProcessJoinFunction<PaymentInfo, OrderWide, PaymentWide>.Context ctx, Collector<PaymentWide> out) throws Exception {
                        out.collect(new PaymentWide(paymentInfo, orderWide));
                    }
                });
        // 5. 将流写回到 kafka 的 dwm_payment_wide 中
        paymentWideInfo.map(JSON::toJSONString)
                .addSink(KafkaUtils.getKafkaSinkAtLeastOnce("dwm_payment_wide"));
        // 6. 触发程序执行
        env.execute();
    }
}
